【Mybatis】Mybatis处理一对多、多对多关系映射

您所在的位置:网站首页 mybatis collection 一对多 【Mybatis】Mybatis处理一对多、多对多关系映射

【Mybatis】Mybatis处理一对多、多对多关系映射

2023-05-18 16:32| 来源: 网络整理| 查看: 265

唠嗑部分

上篇文章我们说了Mybatis中ORM映射的几种方式,相关文章: 【Mybatis】简单入门及工具类封装-一 【Mybatis】如何实现ORM映射-二 【Mybatis】Mybatis的动态SQL、缓存机制-三

这篇文章我们来说说Mybatis如何处理一对一、一对多、多对多的关系映射

首先创建环境,表结构

create database `mybatis-wx-demo` character set 'utf8mb4'; use `mybatis-wx-demo`; create table user ( id int primary key auto_increment comment '主键id', user_name varchar(255) comment '用户名', age int comment '年龄', create_time datetime comment '创建时间' ) comment '用户表'; insert into user values (1, '全栈小白', 23, CURRENT_TIMESTAMP()), (2, '南宫飞雪', 25, CURRENT_TIMESTAMP()), (3, '盒马鲜生', 30, CURRENT_TIMESTAMP()); create table user_account( acc_id int primary key auto_increment comment '账户id', acc_balance int default 0 comment '账户余额', user_id int comment '用户id' ) comment '用户账户表'; insert into user_account values (1, 100, 1); create table role( role_id int primary key auto_increment comment '角色id', role_name varchar(50) comment '角色名' ) comment '角色表'; insert into role values (1, '超级管理员'), (2, '管理员'), (3, '用户'); create table user_role( id int primary key auto_increment comment '主键id', u_id int not null comment '用户id', r_id int not null comment '角色id' ) comment '用户角色关联表'; insert into user_role values (1, 1, 1), (2, 1, 2); create table user_photo( id int primary key auto_increment comment '主键', user_id int not null comment '用户id', url varchar(255) not null comment '用户照片' ) comment '用户照片表'; insert into user_photo values (1, 1, 'https://img2.baidu.com/it/u=3202947311,1179654885&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500'), (2, 1, 'https://lmg.jj20.com/up/allimg/1114/040221103339/210402103339-8-1200.jpg');

说说表结构,user与user_account是一对一的关系,用户只能有一个账户,

user与user_photo是一对多的关系,用户可以有多个生活照片

user与role是多对多的关系,用户可以有多个角色

言归正传

实体类关系就不演示了,着重说数据封装

需求一:查询用户id为1的用户信息及用户账户信息(一对一)

1、创建UserAccountInfoBO业务封装对象

/** * @Project: mybatis-wx-demo * @Author: [email protected] * @Create: 2023/3/10 11:01 * @Description: **/ @Data public class UserAccountInfoBO { private User userInfo; private UserAccount userAccountInfo; }

2、编写接口

/** * @Project: mybatis-wx-demo * @Author: [email protected] * @Create: 2023/2/28 10:41 * @Description: **/ public interface UserMapper { UserAccountInfoBO selectUserAndAccountByUserId(@Param("id") Integer id); }

3、编写xml配置文件

使用association标签处理复杂的javaBean

DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> select * from user u inner join user_account ua on u.id = ua.user_id where u.id = #{id}

4、测试

image-20230310113412445

需求二:查询用户id为1的用户信息及用户的照片信息(一对多)

1、创建UserPhotoInfoBO业务封装对象

/** * @Project: mybatis-wx-demo * @Author: [email protected] * @Create: 2023/3/10 11:40 * @Description: **/ @Data public class UserPhotoInfoBO { private Integer id; private String userName; private Integer age; private LocalDateTime createTime; private List userPhotoList; }

2、编写接口

public interface UserMapper { UserAccountInfoBO selectUserAndAccountByUserId(@Param("id") Integer id); UserPhotoInfoBO selectUserAndPhotoByUserId(@Param("id") Integer id); }

3、编写xml配置文件

使用collection标签封装Bean集合,照片是多个

select u.*, up.id as photo_id, up.user_id, up.url from user u inner join user_photo up on u.id = up.user_id where u.id = #{id}

4、测试

image-20230310131812457

需求三:查询用户id为1的用户信息及用户的角色信息(多对多)

1、创建UserRoleInfoBO业务封装对象

/** * @Project: mybatis-wx-demo * @Author: [email protected] * @Create: 2023/3/10 13:19 * @Description: **/ @Data public class UserRoleInfoBO { private Integer id; private String userName; private Integer age; private LocalDateTime createTime; private List roleList; }

2、编写接口

/** * @Project: mybatis-wx-demo * @Author: [email protected] * @Create: 2023/2/28 10:41 * @Description: **/ public interface UserMapper { UserAccountInfoBO selectUserAndAccountByUserId(@Param("id") Integer id); UserPhotoInfoBO selectUserAndPhotoByUserId(@Param("id") Integer id); UserRoleInfoBO selectUserAndRoleListByUserId(@Param("id") Integer id); }

3、编写xml配置文件

select u.*, r.* from user u left join user_role ur on u.id = ur.u_id inner join role r on ur.r_id = r.role_id where u.id = #{id}

4、测试

image-20230310132450172

结语

1、三种不同的关系映射就说到这,不知大家有没有发现,一对多、多对多很类似,只是sql语句上的不同

2、Mybatis相关内容,后续还会安排动态SQL、缓存及SpringBoot整合Mybatis

3、制作不易、一键三连再走吧,您的支持是我最大的动力!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3